<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\facade\Token;
use app\common\model\AccessToken;
use jueqi\Captcha;
use jueqi\ClickCaptcha;
use jueqi\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Config;
use think\Response;

class Common extends Api
{
    public function captcha(): Response
    {
        $captchaId = $this->request->request('id', '');
        $config    = array(
            'codeSet'  => '0123456789ABCDEFGHJKLMNPQRTUVWXY', // 验证码字符集合
            'fontSize' => 24,  // 验证码字体大小(px)
            'useCurve' => false, // 是否画混淆曲线
            'useNoise' => true, // 是否添加杂点
            'length'   => 4, // 验证码位数
            'imageH'   => 68, // 验证码图片高度
            'bg'       => array(255, 255, 255), // 背景颜色
        );

        $captcha = new Captcha($config);
        return $captcha->entry($captchaId);
    }

    public function clickCaptcha()
    {
        $id      = $this->request->request('id/s');
        $captcha = new ClickCaptcha();
        $this->success('', $captcha->creat($id));
    }

    public function checkClickCaptcha()
    {
        $id      = $this->request->post('id/s');
        $info    = $this->request->post('info/s');
        $unset   = $this->request->post('unset/b', false);
        $captcha = new ClickCaptcha();
        if ($captcha->check($id, $info, $unset)) $this->success();
        $this->error();
    }

    public function refreshToken()
    {
        $refreshTokenName = Config::get("backstage.refresh_token_name");
        $refreshToken     = $this->request->post($refreshTokenName);
        $refreshToken     = Token::get($refreshToken, false);

        if (!$refreshToken || $refreshToken['expire_time'] < time()) {
            $this->error(__('Login expired, please login again.'), '', 302);
        }

        $newToken = Random::uuid();

        if ($refreshToken['type'] == 'admin-refresh') {
            $tokenName          = Config::get("backstage.admin_token_name");
            $serverTokenName    = Config::get("backstage.admin_server_token_name");
            $authorizationToken = $this->request->server($serverTokenName, $this->request->request($tokenName, ''));
            if (!$authorizationToken) {
                $this->error(__('Invalid token'));
            }
            Token::delete($authorizationToken);
            Token::set($newToken, 'admin', $refreshToken['user_id'], 86400);
        } elseif ($refreshToken['type'] == 'user-refresh') {
            $tokenName       = Config::get("backstage.user_token_name");
            $serverTokenName = Config::get("backstage.user_server_token_name");
            $baUserToken     = $this->request->server($serverTokenName, $this->request->request($tokenName, ''));
            if (!$baUserToken) {
                $this->error(__('Invalid token'));
            }
            Token::delete($baUserToken);
            Token::set($newToken, 'user', $refreshToken['user_id'], 86400);
        }

        $this->success('', [
            'type'  => $refreshToken['type'],
            'token' => $newToken
        ]);
    }

}
